package com.mxchip.livestar.ui.contact


import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.mxchip.livestar.R
import com.mxchip.livestar.base.*
import kotlinx.android.synthetic.main.fragment_add_member.*


class AddMemberFragment : DialogFragment() {

    private var contactId: Long = -1L
    private val contactViewModel by viewModels<ContactViewModel>()


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.setCanceledOnTouchOutside(false)

        return dialog
    }


    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_add_member, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val numberArg = arguments?.let { AddMemberFragmentArgs.fromBundle(it).number }
        val nameArg = arguments?.let { AddMemberFragmentArgs.fromBundle(it).name }

        arguments?.let { AddMemberFragmentArgs.fromBundle(it).id }?.let { contactId = it }

        numberArg?.let {
            input_number.setText(it)
        }

        nameArg?.let {
            input_name.setText(it)
        }

        if (contactId == -1L) {
            text_title.text = getString(R.string.new_contact)
        } else {
            text_title.text = getString(R.string.update_contacts)
        }


        save_button.setOnClickListener {
            val name = input_name.text.toString()
            val number = input_number.text.toString()
            if (TextUtils.isEmpty(name)) {
                toast(getString(R.string.name_empty))
                return@setOnClickListener
            } else if (TextUtils.isEmpty(number)) {
                toast(getString(R.string.number_empty))
                return@setOnClickListener
            }
            val dialNumber = if (number.length <= AppConstant.LOCAL_CODE_LENGTH) {
                val fillSize = AppConstant.LOCAL_CODE_LENGTH - number.length
                val sb = StringBuilder()
                for (i in 0 until fillSize) {
                    sb.append("0")
                }
                val userName = sb.toString() + number
                userName
            } else {
                number
            }
            if (SystemData.userId() == dialNumber.trim() ) {
                toast(getString(R.string.called_local_number))
                return@setOnClickListener
            }
            if (SystemData.userId() == dialNumber) {
                toast(getString(R.string.storage_self))
                return@setOnClickListener
            }


            if (contactId == -1L) {
                contactViewModel.createContact(name, dialNumber)
            } else {
                Log.e("testInsert", "testInsert $contactId, $name, $dialNumber")
                contactViewModel.updateContact(contactId, name, dialNumber)
            }

            hideKeyboard()
            findNavController().popBackStack()
        }

        button_cancel.setOnClickListener {
            hideKeyboard()
            findNavController().popBackStack()
        }
    }

}
